package com.skyline2yc.databus.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skyline2yc.databus.common.DataBusConstants;
import com.skyline2yc.databus.entity.DataMarket;
import com.skyline2yc.databus.entity.DataSource;
import com.skyline2yc.databus.entity.PageResult;
import com.skyline2yc.databus.mapper.DataMarketMapper;
import com.skyline2yc.databus.mapper.DataSourceMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class DataMarketService extends ServiceImpl<DataMarketMapper, DataMarket> {

    @Autowired
    private DataMarketMapper dataMarketMapper;

    @Autowired
    private DataSourceMapper dataSourceMapper;

    /**
     * 新建数据集
     *
     * @param datasetName
     * @param tableName
     * @param datasourceName
     * @return
     */
    public Integer createDataMarket(String datasourceName, String datasetName, String tableName, String serviceName) {
        //创建一个数据集对象
        DataMarket dataMarket = new DataMarket();
        //注入属性
        dataMarket.setCreateUser(DataBusConstants.Create_User);
        dataMarket.setUpdateUser(DataBusConstants.Update_User);
        dataMarket.setDatasetName(datasetName);
        dataMarket.setTableName(tableName);
        dataMarket.setSqlSentence(DataBusConstants.Sql_Sentence + tableName);
        dataMarket.setDbName(datasourceName);
        dataMarket.setServiceName(serviceName);

        //获取dbId
        DataSource entity = new DataSource();
        entity.setBeanName(datasourceName);
        QueryWrapper<DataSource> query = Wrappers.query(entity);
        DataSource dataSource = dataSourceMapper.selectOne(query);
        dataMarket.setDbId(dataSource.getId());

        int num = dataMarketMapper.insert(dataMarket);


        return num;
    }


    /**
     * 分页查询数据集信息
     */
    public PageResult<DataMarket> pageQyery(Integer page, Integer rows, String key) {

        //定义一个plus的分页对象
        IPage<DataMarket> iPage = new Page<>(page, rows);
        //创建封装条件对象
        QueryWrapper<DataMarket> queryWrapper = Wrappers.query();
        if (StringUtils.isNotBlank(key)) {
            queryWrapper.like("dataset_name", key)
                        .or()
                        .like("db_name", key);
        }
        //分页查询
        iPage = dataMarketMapper.selectPage(iPage, queryWrapper);
        //创建一个自定义的分页对象
        PageResult<DataMarket> pageResult = new PageResult<>(
                iPage.getTotal(),
                iPage.getPages(),
                iPage.getRecords()

        );
        return pageResult;
    }

    /**
     * 根据id删除数据集
     *
     * @param id
     * @return
     */
    public Integer deleteById(Integer id) {
        Integer count = dataMarketMapper.deleteById(id);
        return count;
    }


    /**
     * 修改数据集
     */
}
